package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zzhoujay.richtext.RichText;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.EmptyRecyclerView;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.KnowCommentAdapter;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.AllKnowBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.FKnowBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserFlagBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.CollectionFKnowModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.FKnowInfoModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetCollectionFKnowFlagModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetFknowCommentListModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetUserFlagModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.RemoveCollectionFKnow;

public class FKnowInfoActivity extends BaseActivity implements View.OnClickListener {

    TextView fknow_info;
    private TextView text;
    private Drawable drawable;
    String fknow_id,userid;
    ImageView shoucangimg,fknow_info_zf;
    ImageView fknow_info_fh;
    private String collectionflag;
    private boolean up = false;
    private Dialog dialog;

    EmptyRecyclerView fknow_info_comment_rv;
    private List<AllKnowBean> items;

    ImageView fknow_info_comment;

    private View mEmptyView;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_fknow_info);
        fknow_info=(TextView)findViewById(R.id.fknow_info);
        shoucangimg = (ImageView)findViewById(R.id.shoucangimg);
        fknow_info_fh = (ImageView)findViewById(R.id.fknow_info_fh);
        fknow_info_comment_rv = (EmptyRecyclerView)findViewById(R.id.fknow_info_comment_rv);
        fknow_info_comment = (ImageView)findViewById(R.id.fknow_info_comment);
        fknow_info_zf = (ImageView)findViewById(R.id.fknow_info_zf);

        mEmptyView = (View)findViewById(R.id.id_empty_comment);

        fknow_id = getIntent().getStringExtra("fknow_id");

        userid = getid();
        dialog = new WeiboDialogUtils().createLoadingDialog(FKnowInfoActivity.this,"加载中...");
        GetCollectionFKnowFlagModel getCollectionFKnowFlagModel = new GetCollectionFKnowFlagModel();
        getCollectionFKnowFlagModel.getCollectionFKnowFlag(userid,fknow_id,successBeanTListener);

        FKnowInfoModel fKnowInfoModel = new FKnowInfoModel();
        fKnowInfoModel.getFKnowInfo(fknow_id,fKnowBeanTListener);

        GetFknowCommentListModel getFknowCommentListModel = new GetFknowCommentListModel();
        getFknowCommentListModel.getFknowCommentList(fknow_id,allKnowBeanListListener);

    }

    @Override
    void initEvent() {
        shoucangimg.setOnClickListener(this);
        fknow_info_comment.setOnClickListener(this);
        fknow_info_fh.setOnClickListener(this);
        fknow_info_zf.setOnClickListener(this);

    }

    @Override
    void initDate() {

    }
    ListListener<AllKnowBean> allKnowBeanListListener = new ListListener<AllKnowBean>() {
        @Override
        public void onResponse(List<AllKnowBean> list) {

            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(FKnowInfoActivity.this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            fknow_info_comment_rv.setLayoutManager(linearLayoutManager);
            items=list;
            KnowCommentAdapter knowCommentAdapter=new KnowCommentAdapter(FKnowInfoActivity.this,items,R.layout.knowcomment_item);
            fknow_info_comment_rv.setAdapter(knowCommentAdapter);
            fknow_info_comment_rv.setEmptyView(mEmptyView);
            fknow_info_comment_rv.setItemAnimator(new DefaultItemAnimator());
            WeiboDialogUtils.closeDialog(dialog);
        }

        @Override
        public void onFail(String msg) {
            WeiboDialogUtils.closeDialog(dialog);
        }
    };
    TListener<FKnowBean> fKnowBeanTListener = new TListener<FKnowBean>() {
        @Override
        public void onResponse(FKnowBean fKnowBean) {
            String fknow_content = fKnowBean.getFknow_content();
            fknow_info.setMovementMethod(LinkMovementMethod.getInstance());
            RichText.fromHtml(fknow_content).into(fknow_info);
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(FKnowInfoActivity.this,"cuowu",Toast.LENGTH_SHORT).show();
        }
    };

    TListener<SuccessBean> successBeanTListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            String success = successBean.getSuccess();
            if (success.equals("1")){
                shoucangimg.setImageResource(R.mipmap.shoucangl);
                collectionflag = "1";
            }
            else{
                shoucangimg.setImageResource(R.mipmap.shoucang);
                collectionflag = "0";
            }
        }

        @Override
        public void onFail(String msg) {
            shoucangimg.setImageResource(R.mipmap.shoucang);
            collectionflag = "0";
            Toast.makeText(FKnowInfoActivity.this,"收藏异常",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.shoucangimg:
                if (collectionflag.equals("0")){
                    CollectionFKnowModel collectionFKnowModel =  new CollectionFKnowModel();
                    collectionFKnowModel.collectionFKnow(userid,fknow_id,successBeanTListener1);
                }
                else if (collectionflag.equals("1"))
            {
                RemoveCollectionFKnow removeCollectionFKnow = new RemoveCollectionFKnow();
                removeCollectionFKnow.removeCollectionFKnow(fknow_id,getid(),successBeanTListener2);
            }
            else{
                    Toast.makeText(FKnowInfoActivity.this,"当前状态不能收藏",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.fknow_info_comment:
                GetUserFlagModel getUserFlagModel = new GetUserFlagModel();
                getUserFlagModel.getUserFlag(getid(), new TListener<UserFlagBean>() {
                    @Override
                    public void onResponse(UserFlagBean userFlagBean) {
                        if (userFlagBean.getUserflag().equals("2")){
                            Toast.makeText(FKnowInfoActivity.this, "当前你已被系统禁止登陆，不可评论任何文章", Toast.LENGTH_SHORT).show();
                        }
                        else if (userFlagBean.getUserflag().equals("1")){
                            Toast.makeText(FKnowInfoActivity.this, "当前你已被系统禁止评论，不可评论任何文章", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Intent intentc = new Intent(FKnowInfoActivity.this,CommentFKnowActivity.class);
                            intentc.putExtra("fknowid",fknow_id);
                            startActivity(intentc);
                        }
                    }
                    @Override
                    public void onFail(String msg) {
                        Toast.makeText(FKnowInfoActivity.this, "？？？"+getid(), Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.fknow_info_fh:
                finish();
                break;
            case R.id.fknow_info_zf:
                Toast.makeText(FKnowInfoActivity.this, "等待开发", Toast.LENGTH_SHORT).show();
                break;

        }
    }
    TListener<SuccessBean> successBeanTListener2 = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            String success = successBean.getSuccess();
            if (success.equals("1")){
                GetCollectionFKnowFlagModel getCollectionFKnowFlagModel = new GetCollectionFKnowFlagModel();
                getCollectionFKnowFlagModel.getCollectionFKnowFlag(userid,fknow_id,successBeanTListener);
                Toast.makeText(FKnowInfoActivity.this,"取消收藏成功",Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(FKnowInfoActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(FKnowInfoActivity.this,"失败1",Toast.LENGTH_SHORT).show();
        }
    };

   TListener<SuccessBean> successBeanTListener1 = new TListener<SuccessBean>() {
       @Override
       public void onResponse(SuccessBean successBean) {
           String success  = successBean.getSuccess();
           if (success.equals("1")){
               GetCollectionFKnowFlagModel getCollectionFKnowFlagModel = new GetCollectionFKnowFlagModel();
               getCollectionFKnowFlagModel.getCollectionFKnowFlag(userid,fknow_id,successBeanTListener);
               Toast.makeText(FKnowInfoActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
           }
           else{
               Toast.makeText(FKnowInfoActivity.this,"失败",Toast.LENGTH_SHORT).show();
           }
       }

       @Override
       public void onFail(String msg) {
           Toast.makeText(FKnowInfoActivity.this,"呜呜呜",Toast.LENGTH_SHORT).show();
       }
   };

    @Override
    public void onPause() {
        super.onPause();
        up = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (up) {
            initView();
            initEvent();
            initDate();
            up = false;
        }
    }
}




